<?php

namespace App\Services\Index\Elasticsearch;

use App\Services\Service;
use App\Services\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info as SettlementInfo;
use App\Core\Traits\Numeral;
use Helper\Timed;

class SummaryService extends Service
{
	use DatabaseElasticsearch, Numeral;

	/**
	 * 概要统计【游戏人数】
	 */
	public function playerGame($request)
	{
		Timed::setForceAbs();

		$timezone = Timed::getTimeCode();

		$params = ['body' => ['size' => 0, 'track_total_hits' => false]];
		// 筛选【类型】
		$this->makeWhere($params, 'player_type', SettlementInfo::PLAYER_TYPE);
		// 筛选【平台】
		$this->makeWhereOrg($request, $params);

		$paramsSum = $params;
		// 筛选【时间】
		$this->makeWhereParamDatetime($request, $paramsSum, 'end_time', 'datetime', null, 0, 0, 'default', false);
		// 统计数据【数据合计】
		$datetime = $this->getParamArray($request, 'datetime');
		$paramsSum['body']['aggs']['group'] = [
			'date_histogram' => [
				'field' => 'end_time_at',
				'time_zone' => "{$timezone}",
				'calendar_interval' => '1d',
				'format' => 'yyyy-MM-dd',
				'min_doc_count' => 0,
				'extended_bounds' => [
					'min' => $datetime[0] * 1000,
					'max' => $datetime[1] * 1000,
				],
			],
			'aggs' => [
				'distinct_uid' => [
					'cardinality' => [
						'field' => 'uid',
						'precision_threshold' => 40000
					]
				],
			],
		];
		// 查询数据
		$results = SettlementInfo::search($paramsSum);
		$tjData = $results['aggregations']['group']['buckets'];
		// unset($tjData[0]);

		$total = 0;
		foreach ($tjData as $k => $v) {
			$total += $v['distinct_uid']['value'];
		}
		// 数据合计
		// $total = $results['aggregations']['distinct_uid']['value'];

		// 筛选【时间】
		$this->makeWhereLastDays($request, $params, 'end_time', 8);
		// 获取时间参数
		$datetime = $this->getParamArray($request, 'datetime');
		// 统计数据【数据列表】
		$params['body']['aggs']['group'] = [
			'date_histogram' => [
				'field' => 'end_time_at',
				'time_zone' => "{$timezone}",
				'calendar_interval' => '1d',
				'format' => 'yyyy-MM-dd',
				'min_doc_count' => 0,
				'extended_bounds' => [
					'min' => $datetime[0] * 1000,
					'max' => $datetime[1] * 1000,
				],
			],
			'aggs' => [
				'distinct_uid' => [
					'cardinality' => [
						'field' => 'uid',
						'precision_threshold' => 40000
					]
				],
			],
		];
		// 查询数据
		$results = SettlementInfo::search($params);
		date_default_timezone_set(Timed::getTimezoneName());
		// 制作数据
		$collection = collect(array_map(
			function ($item) {
				return [
					'timed' => $item['key'] / 1000,
					'amount' => $item['distinct_uid']['value'],
				];
			},
			$results['aggregations']['group']['buckets'] ?? []
		));
		unset($collection[0]);

		return [
			$collection,
			$total
		];
	}

	/**
	 * 概要统计【平台收益】
	 */
	public function amount($request)
	{
		Timed::setForceAbs();

		$timezone = Timed::getTimeCode();
		$params = ['body' => ['size' => 0, 'track_total_hits' => false]];
		// 筛选【类型】
		$this->makeWhere($params, 'player_type', SettlementInfo::PLAYER_TYPE);
		// 筛选【平台】
		$this->makeWhereOrg($request, $params);

		$paramsSum = $params;
		// 筛选【时间】
		$this->makeWhereParamDatetime($request, $paramsSum, 'end_time', 'datetime', null, 0, 0, 'default', false);
		// 统计数据【数据合计】
		$this->makeAgg($paramsSum, 'usd.gain_player', 'sum');
		// 查询数据
		$results = SettlementInfo::search($paramsSum);
		// 数据合计
		$total = -$results['aggregations']['usd.gain_player']['value'];

		// 筛选【时间】
		$this->makeWhereLastDays($request, $params, 'end_time', 8);
		// 获取时间参数
		$datetime = $this->getParamArray($request, 'datetime');
		// 统计数据【数据列表】
		$params['body']['aggs']['group'] = [
			'date_histogram' => [
				'field' => 'end_time_at',
				'time_zone' => "{$timezone}",
				'calendar_interval' => '1d',
				'format' => 'yyyy-MM-dd',
				'min_doc_count' => 0,
				'extended_bounds' => [
					'min' => $datetime[0] * 1000,
					'max' => $datetime[1] * 1000,
				],
			],
			'aggs' => [
				'usd.gain_player' => [
					'sum' => [
						'field' => 'usd.gain_player',
					]
				],
			],
		];
		// 查询数据
		$results = SettlementInfo::search($params);
		date_default_timezone_set(Timed::getTimezoneName());
		// 制作数据
		$collection = collect(array_map(
			function ($item) {
				return [
					'timed' => $item['key'] / 1000,
					'amount' => $this->makeRound(-$item['usd.gain_player']['value']),
				];
			},
			$results['aggregations']['group']['buckets'] ?? []
		));
		unset($collection[0]);

		return [
			$collection,
			$total
		];
	}
}
